<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">施工管理</a>
            <a><cite>项目列表</cite></a>
            <a><cite>施工区列表</cite></a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right;margin-right:10px"
       onclick="window.history.go(-1)" title="后退">
        <i class="iconfontBlod" style="line-height:30px">&#xe74f;</i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">搜索</label>
                                <div class="layui-input-block">
                                    <input type="search" autocomplete="off" class="layui-input" id='searchInput'>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn search" type="button" id='search'>
                                    <i class="iconfont layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type='text/html' id='toolbar'>
    <button class="layui-btn add" type="button" auth='addArea'>添加</button>
    <button type="button" class='batchAdd layui-btn' auth='addArea'>批量添加</button>
</script>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['layer', "table",], function () {
        var table = layui.table;
        var layer = layui.layer;
        // 异步数据加载
        var projectId = getQueryString('id');
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '班组成员列表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/project/project/areaLists', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            method: 'POST',
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            where: {
                projectId: projectId,
                search: "",
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports'],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    {field: '', title: '编号', type: 'numbers', width: '6%'},
                    {field: 'name', title: '楼栋/施工区', width: '8%'},
                    {field: 'area', title: '面积(平方米)', width: '8%'},
                    {field: 'budget', title: '预算', width: '8%'},
                    {
                        field: '', title: '预算单价', width: '8%', templet: function (d) {
                            if(d.area == null || d.budget == null){
                                return '-'
                            }else{
                                var budget = d.budget == null?0:Number(d.budget);
                                var area = d.area == null?0:Number(d.area);
                                return (budget / area).toFixed(2)
                            }
                        }
                    },
                    {
                        field: '', title: '成本', width: '8%', templet: function (d) {
                            if(d.workmanAccounts == null && d.handymanAccounts == null){
                                return '-'
                            }else{
                                var workmanAccounts = d.workmanAccounts == null?0:Number(d.workmanAccounts);
                                var handymanAccounts = d.handymanAccounts == null?0:Number(d.handymanAccounts);
                                return workmanAccounts+handymanAccounts
                            }
                        }
                    },
                    {
                        field: '', title: '成本单价', width: '8%', templet: function (d) {
                            if(d.workmanAccounts || d.handymanAccounts){
                                return ((Number(d.workmanAccounts)+Number(d.handymanAccounts)) / Number(d.area)).toFixed(2)
                            }else{
                                return '-'
                            }
                        }
                    },
                    {field: 'remark', title: '说明'},
                    {field: 'order', title: '排序', width: '6%'},
                    {
                        field: '', title: '操作', templet: function (d) {
                            return `
                            <button class='workSection layui-btn' lay-event="workSection" data-id=${d.id} auth="sectionLists">查看楼层/施工段</button>
                            <button class='edit layui-btn' lay-event="edit" data-id=${d.id} auth="editArea">编辑</button>
                            <button class='delete layui-btn layui-btn-danger' lay-event="delete" data-id=${d.id} auth="delArea">删除</button>
                        `
                        }
                    }
                ]
            ],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            done:function(){
                permission();
            }
        });
        table.on('tool(test)', function (data) {
            if (data.event == 'workSection') {
                var id = data.data.id;
                window.location.href = 'workSection.html?id=' + id + '&projectId=' + projectId;
            } else if (data.event == 'edit') {
                layer.open({
                    type: 1,
                    title: "编辑",
                    area: ['40%', 'auto'],
                    content: `
                        <form action="" class="layui-form layerOpen">
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">名称</label>
                                    <div class="layui-input-block">
                                        <input type='text' id='name' class='layui-input' value=${data.data.name}>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">排序</label>
                                    <div class="layui-input-block">
                                        <input type='number' id='order' min=1 class='layui-input' value=${data.data.order}>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">备注</label>
                                    <div class="layui-input-block">
                                        <input type='text' id='remark' class='layui-input' value=${data.data.remark ? data.data.remark : ''}>
                                    </div>
                                </div>
                            </div>
                        </form>
                    `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    yes: function (index, layero) {
                        $.ajax({
                            url: url + "/project/project/editArea",
                            type: "POST",
                            async: false,
                            data: {
                                id: data.data.id,
                                name: $('#name').val(),
                                remark: $('#remark').val(),
                                order: $('#order').val(),
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('编辑项目', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('编辑项目', r);
                                }
                            }
                        })
                    }
                })
            } else if (data.event == 'delete') {
                layer.open({
                    type: 0,
                    title: "删除",
                    area: ['40%', 'auto'],
                    content: '是否删除此施工区',
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    yes: function (index, layero) {
                        $.ajax({
                            url: url + "/project/project/delArea",
                            type: "POST",
                            async: false,
                            data: {
                                id: data.data.id,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('删除', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('删除', r);
                                }
                            }
                        })
                    }
                })
            }
        });
        //搜索
        $('body').on('click', '#search', function () {
            var search = $(this).parent().prev().find('input').val();
            table.reload('lists', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    token: getToken(),
                    search: search
                }
            })
        })
        $('body').on('click', '.add', function () {
            layer.open({
                type: 1,
                title: "添加",
                area: ['30%', 'auto'],
                content: `
                    <form class='layui-form layerOpen'>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>名称</label>
                                <div class='layui-input-block'>
                                    <input type='text'  class='layui-input'id='name'>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>排序</label>
                                <div class='layui-input-block'>
                                    <input type='number' min=1  class='layui-input' id='order' value='1'>
                                </div>
                            </div>
                        </div>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>备注</label>
                                <div class='layui-input-block'>
                                    <input type='text' class='layui-input' id='remark' value="">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                btn: ['确认', '取消'],
                skin: 'my-skin',
                yes: function (index, layero) {
                    var name = $('#name').val();
                    var order = parseInt($('#order').val());
                    var remark = $('#remakr').val();
                    $.ajax({
                        url: url + "/project/project/addArea",
                        type: "POST",
                        async: false,
                        data: {
                            projectId: projectId,
                            name: name,
                            order: order,
                            remark: remark,
                            token: getToken()
                        },
                        success: function (r) {
                            if (r.code == 100000) {
                                tips('添加施工区', r);
                                setTimeout(function () {
                                    layer.closeAll();
                                    table.reload('lists');
                                }, 2000)
                            } else {
                                tips('添加施工区', r);
                            }
                        }
                    })
                }
            })
        })

        $('body').on('click', '.batchAdd', function () {
            layer.open({
                type: 1,
                title: "批量添加",
                area: ['30%', 'auto'],
                content: `
                    <form class='layui-form layerOpen'>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>数量</label>
                                <div class='layui-input-block'>
                                    <input type='number'  class='layui-input'id='amount' min=0 value="0">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                btn: ['确认', '取消'],
                skin: 'my-skin',
                yes: function (index, layero) {
                    var amount = $('#amount').val();
                    if (amount == 0) {
                        return false
                    }
                    $.ajax({
                        url: url + "/project/project/batchAddArea",
                        type: "POST",
                        async: false,
                        data: {
                            projectId: projectId,
                            amount: amount,
                            token: getToken()
                        },
                        success: function (r) {
                            if (r.code == 100000) {
                                tips('批量添加', r);
                                setTimeout(function () {
                                    layer.closeAll();
                                    table.reload('lists');
                                }, 2000)
                            } else {
                                tips('批量添加', r);
                            }
                        }
                    })
                }
            })
        })
    });

</script>

</html>